#pragma once
#include "imageclip.h"

class SClip
{
private:
	/**
	 * what portion of what image to draw. assuming color at index 0 is to be transparent. 
	 * referenced, not managed
	 */
	ImageClip * m_clip;
	/** where to draw it based on a central point */
	SPoint offset;
	/** used to flip parts of an image */
	char flip, flag, _reserved1, _reserved2;
public:
	SClip():m_clip(0),flip(VSDL_FLIP_NONE),flag(0){}
	SClip(ImageClip * a_clip):m_clip(a_clip),flip(VSDL_FLIP_NONE),flag(0){}
	/** copy method */
	void copy(const SClip & c){m_clip=c.m_clip;offset.set(c.offset);flip=c.flip;flag=c.flag;}
	/** copy constructor */
	SClip(const SClip & c){copy(c);}
	/** must overload the assignment operator to work with custom templated data structs */
	SClip & operator=(const SClip & c){copy(c);return*this;}
	inline ImageClip * getClip(){return m_clip;}
	inline SPoint * getOffset(){return &offset;}
	/** where this clip displays on the sprite */
	inline SRect getRect(){return SRect(offset.x, offset.y, 
		m_clip->clip.getWidth(), m_clip->clip.getHeight());}
	void set(ImageClip * a_clip, const SPoint & a_offset)
	{	m_clip = a_clip;	offset.set(a_offset);	}
	inline bool equals(const SClip & c)const{return m_clip == c.m_clip && offset.equals(c.offset);}
	inline bool operator==(const SClip & c)const{return equals(c);}
	inline void draw(SDL_Surface * a_screen, const SPoint & a_center, 
		const int & a_flip, const int & a_pixelSize) const
	{
		SPoint drawArea(m_clip->clip.w, m_clip->clip.h);
		SPoint flippedOffset = offset.flipped(a_flip, drawArea);
		SPoint position = a_center.sum(
			flippedOffset.product(a_pixelSize));
		int flipped = a_flip|flip;
		m_clip->draw(a_screen, 
			position, 
			flipped, a_pixelSize, ImageClip::DEFAULT_TRANSPARENT_INDEX);
		if(a_pixelSize > 1)
		{
			SDL_Rect r = {position.x,position.y,
				m_clip->clip.w*a_pixelSize,
				m_clip->clip.h*a_pixelSize};
			VSDL_drawRect(a_screen, &r, 0, 1);
		}
	}
	inline void draw(SDL_Surface * a_screen, const SPoint & a_center, 
		const int & a_flip, const int & a_pixelSize, const int & a_edgeColor, const int & a_transparentColor) const
	{
		SPoint drawArea(m_clip->clip.w, m_clip->clip.h);
		SPoint flippedOffset = offset.flipped(a_flip, drawArea);
		SPoint position = a_center.sum(
			flippedOffset.product(a_pixelSize));
		int flipped = a_flip|flip;
		m_clip->draw(a_screen, 
			position, 
			flipped, a_pixelSize, ImageClip::DEFAULT_TRANSPARENT_INDEX);
		if(a_pixelSize > 1)
		{
			SDL_Rect r = {position.x,position.y,
				m_clip->clip.w*a_pixelSize,
				m_clip->clip.h*a_pixelSize};
			r.x = position.x;
			r.y = position.y;
			r.w = 1;
			r.h = 1;
			VSDL_drawRect(a_screen, &r, a_edgeColor, 1);
		}
	}
	/**
	 * @param a_flip an SDL_FLIP_* variable to modify how the destination image is drawn
	 */
	void applyFlip(const int & a_flip){flip ^= a_flip;}

	/**
	 * @param a_flip an SDL_FLIP_* variable to modify how the destination image is drawn
	 */
	void setClip(const int & a_flip){flip = a_flip;}
};

